<template>
  <div class="deviceWrapper">
    <el-row type="flex" align="middle">
      <el-col style="margin-left: 20px; margin-right: -20px" :span="2">设备标识:</el-col>
      <el-col :span="6">
        <el-input
          placeholder="请输入设备标识"
          v-model="deviceName" clearable>
        </el-input>
      </el-col>
      <el-col style="margin-left: 20px; margin-right: -20px" :span="2">设备IP:</el-col>
      <el-col :span="6">
        <el-input
          placeholder="请输入设备IP"
          v-model="deviceIp" clearable>
        </el-input>
      </el-col>
      <el-col :span="8" style="text-align:right">
        <el-button type="primary" icon="el-icon-search" @click="searchBtn">搜索</el-button>
        <el-button type="success" icon="el-icon-close" @click="clearConditionBtn">清空条件</el-button>
      </el-col>
    </el-row>
    <div class="delete">
      <el-button type="primary" icon="el-icon-plus" @click="addBtn">新增</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="deleteBtn">删除</el-button>
    </div>
    <el-table
      :height="tableHeight"
      @selection-change="selectChange"
      stripe
      :data="deviceList"
      style="width: 100%;">
       <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="55">
      </el-table-column>
      <el-table-column
        type="index"
        label="序号"
        header-align="center"
        align="center"
        width="200">
      </el-table-column>
      <el-table-column
        label="设备标识"
        header-align="center"
        align="center"
        width="150">
        <template slot-scope="scope">
          <el-button @click="changeDevice(scope.row)" type="text">{{scope.row.deviceName}}</el-button>
        </template>
      </el-table-column>
      <el-table-column
        prop="deviceIp"
        label="设备IP"
        header-align="center"
        align="center">
      </el-table-column>
      <el-table-column
        label="标签"
        header-align="center"
        align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag 
              :type="scope.row.id !== -1 ? 'primary' : 'success'"
              size="medium">{{ scope.row.id === -1 ? '默认' : '自定义' }}</el-tag>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogName" :visible.sync="dialogFormVisible" width="450px" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <el-form :model="device">
        <el-form-item label="设备标识:" :label-width="formLabelWidth">
          <el-input v-model="device.deviceName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="设备IP:" :label-width="formLabelWidth">
          <el-input v-model="device.deviceIp" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAdd">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      deviceList: [],
      deviceName: '',
      deviceIp: '',
      selection: [],
      tableHeight: 0,
      total: 0,
      pageNum: 1,
      pageSize: 10,
      dialogFormVisible: false,
      device: {
        deviceName: '',
        deviceIp: '',
        id: ''
      },
      formLabelWidth: '100px',
      dialogName: ''
    }
  },
  created() {
    this._getDeviceList()
    this.tableHeight = window.innerHeight - 370
  },
  components: {
  },
  methods: {
    _getDeviceList() {
      this.$http.get("/v1/device",{
        deviceName: this.deviceName,
        deviceIp: this.deviceIp,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }).then((res) => {
        this.deviceList = res.data
        this.total = res.total
      }).catch((errMsg) => {
        this.$message.error(errMsg)
      })
      this.selection = []
    },
    selectChange(selection) {
      this.selection = selection
    },
    searchBtn() {
      this._getDeviceList()
    },
    clearConditionBtn() {
      this.deviceName = ''
      this.deviceIp =''
      this._getDeviceList()
    },
    deleteBtn() {
      if(this.selection.length === 0){
        this.$message.error("请选择要删除的数据")
        return
      }
      let hasDefault = false
      this.selection.forEach(item => {
        if(item.id === -1){
          this.$message.error("默认设备不允许删除")
          hasDefault= true
        }
      })
      if(hasDefault){
        return
      }
      this.$confirm('此操作将删除所选数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = []
          this.selection.forEach(item => {
            ids.push(item.id)
          })
          this.$http.delete("/v1/device",{
            ids: ids.join(",")
          }).then((res) => {
            this.$message({
              message: '成功删除' + res.data + '条数据',
              type: 'success'
            })
            this.deviceList = this.deviceList.filter(device => ids.indexOf(device.id) === -1)
            this._getDeviceList()
          }).catch((errMsg) => {
            this.$message.error(errMsg)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addBtn() {
      this.dialogFormVisible = true
      this.dialogName = '新增设备'
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this._getDeviceList()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this._getDeviceList()
    },
    cancelAdd() {
      this.device.deviceName = ''
      this.device.deviceIp = ''
      this.device.id = ''
      this.dialogFormVisible = false
    },
    sureAdd() {
      if(this.device.deviceIp == null || this.device.deviceName == null){
        this.$message.error('设备标识与设备IP均不能为空')
        return
      }else if(!this.isValidIp(this.device.deviceIp)){
        this.$message.error('设备IP格式不正确')
        return
      }
      if(this.device.id == null || this.device.id === '') {
        this.$http.post('/v1/device',{
          deviceName: this.device.deviceName,
          deviceIp: this.device.deviceIp
        }).then(() => {
          this.device.deviceName = ''
          this.device.deviceIp = ''
          this.device.id = ''
          this.dialogFormVisible = false
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this._getDeviceList()
        }).catch((errMsg) => {
          this.$message.error(errMsg)
        })
      } else {
        this.$http.put('/v1/device/' + this.device.id,{
          deviceName: this.device.deviceName,
          deviceIp: this.device.deviceIp
        }).then(() => {
          this.device.deviceName = ''
          this.device.deviceIp = ''
          this.device.id = ''
          this.dialogFormVisible = false
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this._getDeviceList()
        }).catch((errMsg) => {
          this.$message.error(errMsg)
        })
      }
    },
    isValidIp(ip) {
      var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      return reg.test(ip)
    },
    changeDevice(device) {
      if(device.id === -1) {
        this.$message.error("默认设备不允许修改")
      } else {
        this.device.deviceName = device.deviceName
        this.device.deviceIp = device.deviceIp
        this.device.id = device.id
        this.dialogName = '修改设备'
        this.dialogFormVisible = true
      }
    }
  }
}
</script>

<style scoped lang="stylus">
.deviceWrapper {
  z-index 20
}
.condition
  display flex
  flex-direction row
  align-items center
  justify-content space-between
  span
    margin-left 36px
    margin-right 12px
  .el-input
    width 220px
  .el-button
    margin-right 4px
.delete
  text-align left 
  margin-top 12px
  margin-bottom 12px
  margin-left 36px
.el-pagination
  margin-top 16px
  text-align right
</style>
